package com.finace.money.ui.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.MessageSendUtil;
import com.finace.money.common.util.PopuUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.BankCradInfoBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.ui.widget.MyPopupWindow;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/11/28.
 */

public class PersonInfoActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.tv_nickname)
    TextView tvNickname;
    @BindView(R.id.rl_nickname)
    RelativeLayout rlNickname;
    @BindView(R.id.tv_person_info_bank_info)
    TextView tvPersonInfoBankInfo;
    @BindView(R.id.rl_person_info_bind_real_name)
    RelativeLayout rlPersonInfoBindRealName;
    @BindView(R.id.tv_phone_check)
    TextView tvPhoneCheck;
    @BindView(R.id.rl_phone_check)
    RelativeLayout rlPhoneCheck;
    @BindView(R.id.rl_passward_modify)
    RelativeLayout rlPasswardModify;
    @BindView(R.id.rl_change_info)
    RelativeLayout rlChangeInfo;
    private EditText etPopupNickname;
    private MyPopupWindow myPopupWindow;
    private int mBankFK = 0;

    @Override
    public int setLayout() {
        return R.layout.activity_person_info;
    }

    @Override
    public void init() {
        MessageSendUtil.QryContractBank();
        tvHeadTitle.setText(UIUtil.getString(R.string.person_info));
        rlLeft.setOnClickListener(this);
//        rlNickname.setOnClickListener(this);
        rlPersonInfoBindRealName.setOnClickListener(this);
        rlPhoneCheck.setOnClickListener(this);
        rlPasswardModify.setOnClickListener(this);
        rlChangeInfo.setOnClickListener(this);
        getAcahe();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_INFO_UPDATE);
        LocalBroadcastManager.getInstance(this).registerReceiver(broadcastReceiver, intentFilter);
        unregisterReceiver(broadcastReceiver);
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            getAcahe();
        }
    };

    private void getAcahe() {

        BankCradInfoBean bankCradInfoBean = (BankCradInfoBean) ACache.get(UIUtil.getContext()).getAsObject(Constant.BANK_CARD_INFO);
        if(null != bankCradInfoBean){
            mBankFK = bankCradInfoBean.getBankFK();
            if(mBankFK != 0){
                //绑定过
                tvPersonInfoBankInfo.setText(bankCradInfoBean.getBankAC());
            }
            tvNickname.setText(bankCradInfoBean.getName());
            tvPhoneCheck.setText(bankCradInfoBean.getMobile());
        }
    }

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_left:
                onBackPressed();
                break;
            /*case R.id.rl_nickname:
                changeNickName();
                break;*/
            case R.id.rl_person_info_bind_real_name:
                Intent intentBank = new Intent(this, BindCardRealNameActivity.class);
                intentBank.putExtra(Constant.BANK_CARD_INFO,mBankFK);
                startActivityForResult(intentBank,0);
                break;
            case R.id.rl_phone_check:
                break;
            case R.id.rl_passward_modify:
                Intent intentMofiy = new Intent(this, ModifyPasswardActivity.class);
                startActivity(intentMofiy);
                break;
            case R.id.button_creat_cancel:
                myPopupWindow.dismiss();
                break;
            case R.id.button_creat_commit_sure:
                if(StringUtil.isNotNullString(etPopupNickname.getText().toString().trim())){
                    tvNickname.setText(etPopupNickname.getText().toString().trim());
                    myPopupWindow.dismiss();
                }else {
                    UIUtil.showToastSafe(getString(R.string.nickname_input_please));
                }
                break;
            case R.id.rl_change_info:
                Intent intentChange = new Intent(this, BindCardRealNameActivity.class);
                intentChange.putExtra(Constant.BANK_CARD_INFO,0);
                startActivityForResult(intentChange,0);
                break;

        }
    }

    private void changeNickName() {

        myPopupWindow = new PopuUtil().initAtLocationPopuWrap(this,
                R.layout.popup_nickname, llContent, 265, 152);
        etPopupNickname = myPopupWindow.getContentView().findViewById(R.id.et_popup_nickname);
        etPopupNickname.setText(tvNickname.getText().toString());
        Button buttonPopupCancel = myPopupWindow.getContentView().findViewById(R.id.button_creat_cancel);
        Button buttonPopupCommitSure = myPopupWindow.getContentView().findViewById(R.id.button_creat_commit_sure);
        buttonPopupCancel.setOnClickListener(this);
        buttonPopupCommitSure.setOnClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getAcahe();
    }
}
